package com.laivi.basic.model.crawler;

import java.util.HashSet;
import java.util.Set;

import com.laivi.basic.model.datastruct.Queue;

/**
 * @author laivi.zhu@gmail.com
 * @date 2014-5-20
 * description:
 */
public class LinkSet {
	//已访问的 url 集合
    private  Set<String> visitedUrl = new HashSet<String>();
    //待访问的 url 集合
    private  Queue<String> unVisitedUrl = new Queue<String>();
    
    public  Queue<String> getUnVisitedUrl() {
        return unVisitedUrl;
    }

    public synchronized  void addVisitedUrl(String url) {
        visitedUrl.add(url);
    }

    public  void removeVisitedUrl(String url) {
        visitedUrl.remove(url);
    }

    public  String unVisitedUrlDeQueue() {
        return unVisitedUrl.deQueue();
    }

    // 保证每个 url 只被访问一次
    public  void addUnvisitedUrl(String url) {
        if (url != null && !url.trim().equals("") && !visitedUrl.contains(url) && !unVisitedUrl.contians(url)) {
            unVisitedUrl.enQueue(url);
        }
    }
    
    public  boolean isVisited(String url){
    	return visitedUrl.contains(url);
    }

    public  int getVisitedUrlNum() {
        return visitedUrl.size();
    }

    public  boolean unVisitedUrlsEmpty() {
        return unVisitedUrl.isEmpty();
    }
    
    public  void clean(){
    	visitedUrl.clear();
    	unVisitedUrl.clear();
    }
}
